// IniReader by 3nigma
// Server Author: SkyNeT
// Data: 25/07/2013

#include <iostream>
#include <string>
#include <WinSock2.h>

#include "Global.h"
#include "IniReader.h"
#include "Functions.h"

#pragma comment(lib,"ws2_32.lib")

int main()
{
	NT_IniReader *Config = new NT_IniReader("login");
	std::string Ip = Config->StringValue("SETTINGS","$ip","192.168.1.89");
	int Port = Config->IntegerValue("SETTINGS","$port",4003);

	Sock = new SOCKET[CMAX];
	Connections = 0;

	WSAData wsa;
	WSAStartup(0x0201,&wsa);
	
	SOCKET Sock_ls = socket(AF_INET, SOCK_STREAM, 0);

	SOCKADDR_IN Sock_Addr;
	Sock_Addr.sin_addr.s_addr = inet_addr(Ip.c_str());
	Sock_Addr.sin_family = AF_INET;
	Sock_Addr.sin_port = htons(Port);

	bind(Sock_ls,(SOCKADDR*)&Sock_Addr,sizeof(Sock_Addr));

	int ls_result = 0, sin_size = 0;

	while(ls_result >= 0)
	{
		ls_result = listen(Sock_ls,SOMAXCONN);

		if(ls_result < 0)
		{
			WSACleanup();
			std::cout << "error found in the listen !" << std::endl;
		}
		else
		{
			sin_size = sizeof(struct sockaddr_in);
			Sock[Connections] = accept(Sock_ls,(SOCKADDR*)&Sock_Addr,&sin_size);

			std::cout << "client connected." << std::endl;
		}
	}

	destroy(true, 0);
}